Hapi.js is a small Node framework for developing back end web apps.
In this article, we’ll look at how to create back end apps with Hapi.js.
Conflict
We can return a 409 response ith @hapi/boom
with the Boom.conflict
method by writing:
const Hapi = require('@hapi/hapi');
const Boom = require('@hapi/boom');
const init = async () => {
const server = new Hapi.Server({
port: 3000,
host: '0.0.0.0'
});
server.route({
method: 'GET',
path: '/',
handler(request, h) {
throw Boom.conflict('conflict');
}
});
await server.start();
console.log('Server running at:', server.info.uri);
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();
Then when we go to the /
route, we get:
{"statusCode":409,"error":"Conflict","message":"conflict"}
as the response.
Resource Gone Error
We can return the resource gone error by writing:
const Hapi = require('@hapi/hapi');
const Boom = require('@hapi/boom');
const init = async () => {
const server = new Hapi.Server({
port: 3000,
host: '0.0.0.0'
});
server.route({
method: 'GET',
path: '/',
handler(request, h) {
throw Boom.resourceGone('gone');
}
});
await server.start();
console.log('Server running at:', server.info.uri);
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();
We call Boom.resourceGone
to create the error object. The argument we pass in is the message.
Then we get:
{"statusCode":410,"error":"Gone","message":"gone"}
as the response when we go to /
.
Length Required
We call the Boom.lengthRequired
method to return a 411 response.
For example, we can write:
const Hapi = require('@hapi/hapi');
const Boom = require('@hapi/boom');
const init = async () => {
const server = new Hapi.Server({
port: 3000,
host: '0.0.0.0'
});
server.route({
method: 'GET',
path: '/',
handler(request, h) {
throw Boom.lengthRequired('length required');
}
});
await server.start();
console.log('Server running at:', server.info.uri);
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();
We pass in the message as the argument.
Then we get:
{"statusCode":411,"error":"Length Required","message":"length required"}
as the response.
Precondition Failed Error
We can call Boom.preconditionFailed
to returns a 412 response.
For instance, we can write:
const Hapi = require('@hapi/hapi');
const Boom = require('@hapi/boom');
const init = async () => {
const server = new Hapi.Server({
port: 3000,
host: '0.0.0.0'
});
server.route({
method: 'GET',
path: '/',
handler(request, h) {
throw Boom.preconditionFailed('precondition failed');
}
});
await server.start();
console.log('Server running at:', server.info.uri);
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();
to pass in a message.
Entity Too Large
To return a 413 entity too large response, we call Boom.entityTooLarge
.
For instance, we can write:
const Hapi = require('@hapi/hapi');
const Boom = require('@hapi/boom');
const init = async () => {
const server = new Hapi.Server({
port: 3000,
host: '0.0.0.0'
});
server.route({
method: 'GET',
path: '/',
handler(request, h) {
throw Boom.entityTooLarge('too big');
}
});
await server.start();
console.log('Server running at:', server.info.uri);
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();
to throw the error.
And we get:
{"statusCode":413,"error":"Request Entity Too Large","message":"too big"}
as the response.
Unsupported Media Type
We can call Boom.unsupportedMediaType
to throw a 415 unsupported media type error:
const Hapi = require('@hapi/hapi');
const Boom = require('@hapi/boom');
const init = async () => {
const server = new Hapi.Server({
port: 3000,
host: '0.0.0.0'
});
server.route({
method: 'GET',
path: '/',
handler(request, h) {
throw Boom.unsupportedMediaType('that media is not supported');
}
});
await server.start();
console.log('Server running at:', server.info.uri);
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();
Then we get:
{"statusCode":415,"error":"Unsupported Media Type","message":"that media is not supported"}
as the response.
Conclusion
We can return kinds of error responses with Hapi Boom.